package com.huang.leetcode.queue;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：CreateSequence
 * @Date：2020-07-29 12:25
 * @Description：电话号码的字母组合 题解：https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/solution/tong-su-yi-dong-dong-hua-yan-shi-17-dian-hua-hao-m/
 */
public class PB17 {

    public List<String> letterCombinations(String digits) {
        List<String> queue = new ArrayList<>();
        if (digits.length() == 0) {
            return queue;
        }
        queue.add("");

        //键位映射
        String[] letters = {" ","*","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

        //一个一个的处理输入数字
        for (int i = 0; i < digits.length(); i++) {
            //获取数字对应的字符
            String letter = letters[digits.charAt(i)-'0'];
            //计算队列长度，由于每次组合都会去掉一个队尾元素，故需重新获取长度
            int size = queue.size();
            //将队列中的每个元素挨个拿出来
            for (int j = 0; j < size; j++) {
                //每次都取出队尾的字符
                //temp即队尾元素就是上一次输入的字符
                String temp = queue.remove(0);
                //让队尾的字符与当前字符一个一个组合
                for (int k = 0; k < letter.length(); k++) {
                    //添加至队列
                    queue.add(temp + letter.charAt(k));
                }
            }
        }

        return queue;
    }

}
